package com.myhome.service.impl;

import com.myhome.entity.Admin;
import com.myhome.entity.Menu;
import com.myhome.mapper.MenuMapper;
import com.myhome.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-09-04
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public List<Menu> getMenuByUserId() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer adminId = ((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        List<Menu> result = (List<Menu>) valueOperations.get("Menu_" + adminId);
        if (null == result) {
            result = menuMapper.getMenuByUserId(adminId);
            valueOperations.set("Menu_"+adminId,result);
        }
        return result;
    }

    @Override
    public List<Menu> getMenusWithRoles() {
        return menuMapper.getMenusWithRoles();
    }

    @Override
    public List<Menu> getAllmenus() {
        return menuMapper.getAllMenus();
    }


}
